package com.raptor.redis.config;

import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.util.StringUtils;

import java.util.*;


public class RedisConfig {
    private final PropertiesPropertySource propertiesPropertySource;

    private final String PREFIX = "raptor.redis.";

    public RedisConfig(PropertiesPropertySource propertiesPropertySource) {
        this.propertiesPropertySource = propertiesPropertySource;
    }

    public List<RedisConfigBean> GetRedisConfig() {
        String[] propertyNames = this.propertiesPropertySource.getPropertyNames();

        if (propertyNames.length > 0) {
            // instances
            HashMap<String, String> instances = this.buildInstance(propertyNames);
            // properties
            return this.buildProperties(propertyNames, instances);
        }

        return null;
    }

    /*
     * 构建Redis实例
     */
    private HashMap<String,String> buildInstance(String[] propertyNames) {

        HashMap<String,String> instances = new HashMap<>();

        Arrays.asList(propertyNames).forEach(name -> {
            if (!name.startsWith(PREFIX)) {
                return ;
            }
            String[] nameSplit = name.split("\\.");

            String instance = nameSplit[3] + "-" + nameSplit[4];
            instances.put(instance, instance);
        });

        return instances;
    }

    private List<RedisConfigBean> buildProperties(String[] propertyNames, HashMap<String,String> instances) {

        List<RedisConfigBean> beans = new ArrayList<>();

        for (String instance : instances.keySet()) {
            String[] splitStr = instance.split("-");
            String newPrefix = "raptor.redis." + splitStr[0] + "." + splitStr[1];
            RedisConfigBean bean = new RedisConfigBean();
            Arrays.asList(propertyNames).forEach(name -> {
                if (!name.startsWith(newPrefix)) {
                    return ;
                }

                String[] nameSplit = name.split("\\.");
                bean.setInstanceName(instance);

                String property = (nameSplit[5].equals("pool") ? nameSplit[6]: nameSplit[5]);

                switch (property) {
                    case "host":
                        String host = (String) this.propertiesPropertySource.getProperty(name);
                        bean.setHost(host);
                        break;
                    case "port":
                        int port =  Integer.parseInt((String) Objects.requireNonNull(this.propertiesPropertySource.getProperty(name)));
                        bean.setPort(port);
                        break;
                    case "password":
                        String password = (String) this.propertiesPropertySource.getProperty(name);
                        bean.setPassword(password);
                        break;
                    case "timeout":
                        int timeout = Integer.parseInt((String) Objects.requireNonNull(this.propertiesPropertySource.getProperty(name)));
                        bean.setTimeout(timeout);
                        break;
                    case "maxactive":
                        int maxactive = Integer.parseInt((String) Objects.requireNonNull(this.propertiesPropertySource.getProperty(name)));
                        bean.setMaxactive(maxactive);
                        break;
                    case "maxwait":
                        int maxwait = Integer.parseInt((String) Objects.requireNonNull(this.propertiesPropertySource.getProperty(name)));
                        bean.setMaxwait(maxwait);
                        break;
                    case "maxidle":
                        int maxidle = Integer.parseInt((String) Objects.requireNonNull(this.propertiesPropertySource.getProperty(name)));
                        bean.setMaxidle(maxidle);
                        break;
                    case "minidle":
                        int minidle = Integer.parseInt((String) Objects.requireNonNull(this.propertiesPropertySource.getProperty(name)));
                        bean.setMinidle(minidle);
                        break;
                }
            });
            if (!StringUtils.isEmpty(bean.instanceName)) {
                beans.add(bean);
            }
        }

        return beans;
    }
}
